# -*- coding: utf-8 -*-
from wiki.models import *
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required

menu = MainMenu.objects.all()
news = News.objects.all()

def home(request):
	user = request.user
	page_name = 'home'	
	menu_name = 'main'	
	html = Page.objects.get(code=page_name)
	return render_to_response('wiki_view.html', {'page_name':page_name, 
'menu_name':menu_name, 'html': html, 'menu': menu, 'news': news, 'user':user })

def view_page(request, menu_name, page_name):
	if page_name in specialPages:
		return specialPages[page_name](request, menu_name)
	user = request.user
    	try:
		html = Page.objects.get(code=page_name)
	except Page.DoesNotExist:
         	return render_to_response('wiki_add.html', {
'page_name':page_name, 'menu_name':menu_name, 'menu': menu, 'news': news, 
'user':user})	
	return render_to_response('wiki_view.html', {'page_name':page_name,
'menu_name':menu_name, 'html': html, 'menu': menu, 'news': news, 
'user':user })
   
@login_required
def edit_page(request, menu_name, page_name):
	try:
		page = Page.objects.get(code=page_name)
		code = page.code
		title = page.title
		description = page.description
		keywords = page.keywords
		content = page.content
	except Page.DoesNotExist:
		code = page_name
		title = ''
		description = ''
		keywords = ''
		content = ''
	return render_to_response('wiki_edit.html', {'page_name':page_name, 
'menu_name':menu_name, 'menu': menu, 'news': news, 'code':code, 'title':title, 
'description':description, 'keywords':keywords, 'content':content })

@login_required
def save_page(request, menu_name, page_name):
        code = request.POST['code']
        title = request.POST['title']
        description = request.POST['description']
        keywords = request.POST['keywords']
	content = request.POST['content']
	try:
		page = Page.objects.get(code=page_name)
		page.code = code
		page.title = title
		page.description = description
		page.keywords = keywords
		page.content = content
	except Page.DoesNotExist:
		page = Page(code=page_name, title=title, 
description=description, keywords=keywords, content=content)
	page.save()
	page_name = code
	return HttpResponseRedirect('/' + menu_name + '/' + page_name + '/')

def instrumentsall(request, menu_name):
    page_name = 'instrumentsall'
    seo = Page.objects.get(code=page_name)
    html = Instrument.objects.all().order_by('firm', 'name')
    return render_to_response('instrumentsall.html', { 'page_name': page_name,
'menu_name':menu_name, 'html': html, 'seo': seo, 'menu': menu, 'news': news, })    

def articlesall(request, menu_name):
    page_name = 'articlesall'
    seo = Page.objects.get(code=page_name)
    html = Article.objects.all()
    return render_to_response('articlesall.html', { 'page_name': page_name, 
'menu_name':menu_name, 'html': html, 'seo': seo, 'menu': menu, 'news': news, })    

def techsall(request, menu_name):
    page_name = 'techsall'
    seo = Page.objects.get(code=page_name)
    html = Tech.objects.all()
    return render_to_response('techsall.html', { 'page_name':page_name, 
'menu_name':menu_name, 'html': html, 'seo': seo, 'menu': menu, 'news': news, })    

def newsall(request, menu_name):
    page_name = 'newsall'
    seo = Page.objects.get(code=page_name)
    html = News.objects.all()
    return render_to_response('newsall.html', { 'page_name': page_name, 
'menu_name':menu_name, 'html': html, 'seo': seo,'menu': menu, 'news': news, })    

specialPages = {'articlesall': articlesall,  'instrumentsall': instrumentsall, 
'techsall': techsall, 'newsall': newsall }

# ARTICLES

def article_view(request, menu_name, page_name):
	user = request.user
	try:
		html = Article.objects.get(code=page_name)
	except Article.DoesNotExist:
		return render_to_response('article_add.html', {
'page_name':page_name, 'menu_name':menu_name, 'menu': menu, 'news': news, 
'user':user})
	return render_to_response('article_view.html', {'page_name':page_name, 
'menu_name':menu_name, 'html': html, 'menu': menu, 'news': news, 'user':user})

@login_required
def article_edit(request, menu_name, page_name):
	try:
		article = Article.objects.get(code=page_name)
		code = article.code
		title = article.title
		description = article.description
		keywords = article.keywords
		content = article.content
		author = article.author
		publication = article.publication
#		authors = article.authors
#		publisher = article.publisher
#		publication_date = article.publication_date
	except Article.DoesNotExist:
		code = page_name
		title = ''
		description = ''
		keywords = ''
		content = ''
		author = ''
		publication = ''
#		authors = ''
#		publisher = ''
#		publication_date = ''
	return render_to_response('article_edit.html', { 'page_name':page_name, 
'menu_name':menu_name, 'code':code, 'title':title, 'content':content, 
'description':description, 'keywords':keywords, 'author':author, 
'publication':publication, 'menu': menu, 'news': news, 
# 'publisher':publisher,
#, 'authors':authors,
# 'firm':firm,                                                                                                                
# 'publication_date':publication_date,
})

@login_required
def article_save(request, menu_name, page_name):
	code = request.POST['code']
	title = request.POST['title']
        description = request.POST['description']
        keywords = request.POST['keywords']
	content = request.POST['content']
	author = request.POST['author']
	publication = request.POST['publication']
#	authors = request.POST['authors']
#	publisher = request.POST['publisher']
#	publication_date = request.POST['publication_date']
	try:
		article = Article.objects.get(code=page_name)
		article.code = code
		article.title = title
		article.description = description
		article.keywords = keywords
		article.content = content
		article.author = author
		article.publication = publication  
#		article.authors = authors
#		article.publisher = publisher  
#		article.publication_date = publication_date  
	except Article.DoesNotExist:
		article = Article(code=page_name, title=title, content=content, 
description=description, keywords=keywords, author=author, 
publication=publication, 
# publisher=publisher, 
# authors=authors,
# publication_date=publication_date
)
	article.save()
	page_name = code
	return HttpResponseRedirect('/' + menu_name + '/articles/' + page_name + '/')
    


# INSTUMENTS

def instrument_view(request, menu_name, page_name):
	user = request.user
	try:
		html = Instrument.objects.get(code=page_name)
	except Instrument.DoesNotExist:
		return render_to_response('instrument_add.html', { 
'page_name':page_name, 'menu_name':menu_name, 'menu': menu, 'news': news, 'user':user})
	return render_to_response('instrument_view.html', {
'page_name':page_name, 'menu_name':menu_name, 'html': html, 'menu': menu, 'news': news, 
'user':user})

@login_required
def instrument_edit(request, menu_name, page_name):
	try:
		instrument = Instrument.objects.get(code=page_name)
		code = instrument.code
		name = instrument.name
#		firm = instrument.firm
		description = instrument.description
		keywords = instrument.keywords
#		status = instrument.status
#		driver = instrument.driver
		content = instrument.content
	except Instrument.DoesNotExist:
		code = page_name
		name = ''
#		firm = ''
		description = ''
		keywords = ''
#		status = ''
#		driver = ''
		content = ''
	return render_to_response('instrument_edit.html', {
'page_name':page_name, 'menu_name':menu_name, 'code':code, 'name':name, 
#'firm':firm,
'description':description, 'keywords':keywords,
#'status':status, 'driver':driver, 
'menu': menu, 'news': news, 'content':content})

@login_required
def instrument_save(request, menu_name, page_name):
	code = request.POST['code']
	name = request.POST['name']
#	firm = request.POST['firm']
        description = request.POST['description']
        keywords = request.POST['keywords']
#	status = request.POST['status']
#	driver = request.POST['driver']
	content = request.POST['content']
	try:
		instrument = Instrument.objects.get(code=page_name)
		instrument.code = code
		instrument.name = name
#		instrument.firm = firm
		instrument.description = description
		instrument.keywords = keywords
#		instrument.status = status
#		instrument.driver = driver  
		instrument.content = content  
	except Instrument.DoesNotExist:
		instrument = Instrument(code=page_name, name=name, 
#firm=firm, 
description=description, keywords=keywords,
#status=status, driver=driver,
content=content)
	instrument.save()
	page_name = code
	return HttpResponseRedirect('/' + menu_name + '/instruments/' + page_name + '/')
    
# TECH

def tech_view(request, menu_name, page_name):
	user = request.user
	try:
		html = Tech.objects.get(code=page_name)
	except Tech.DoesNotExist:
		return render_to_response('tech_add.html', {
'page_name':page_name, 'menu_name':menu_name, 'menu': menu, 'news': news, 'user':user})
	return render_to_response('tech_view.html', {'page_name':page_name, 
'menu_name':menu_name, 'html': html, 'menu': menu, 'news': news, 'user':user})

@login_required
def tech_edit(request, menu_name, page_name):
	try:
		tech = Tech.objects.get(code=page_name)
		code = tech.code
		title = tech.title
		description = tech.description
		keywords = tech.keywords
		content = tech.content
	except Tech.DoesNotExist:
		code = page_name
		title = ''
		description = ''
		keywords = ''
		content = ''
	return render_to_response('tech_edit.html', {'page_name':page_name, 
'menu_name':menu_name, 'code':code, 'title':title, 
'description':description, 'keywords':keywords, 'menu': menu, 'news': news, 
'content':content})

@login_required
def tech_save(request, menu_name, page_name):
	code = request.POST['code']
	title = request.POST['title']
        description = request.POST['description']
        keywords = request.POST['keywords']
	content = request.POST['content']
	try:
		tech = Tech.objects.get(code=page_name)
		tech.code = code
		tech.title = title
		tech.description = description
		tech.keywords = keywords
		tech.content = content
	except Tech.DoesNotExist:
		tech = Tech(code=page_name, title=title, content=content)
	tech.save()
	page_name = code
	return HttpResponseRedirect('/' + menu_name + '/tech/' + page_name + '/')
    
# NEWS

def news_view(request, menu_name, page_name):
	user = request.user
	try:
		html = News.objects.get(code=page_name)
	except News.DoesNotExist:
		return render_to_response('news_add.html', {
'page_name':page_name, 'menu_name':menu_name, 'menu': menu, 'news': news, 
'user':user})
	return render_to_response('news_view.html', {'page_name':page_name, 
'menu_name':menu_name, 'html': html, 'menu': menu, 'news': news, 'user':user})

@login_required
def news_edit(request, menu_name, page_name):
	try:
		news = News.objects.get(code=page_name)
		code = news.code
		title = news.title
		subject = news.subject
		description = news.description
		keywords = news.keywords
		content = news.content
#		publication_date = news.publication_date
	except News.DoesNotExist:
		code = page_name
		title = ''
                subject = ''
		description = ''
		keywords = ''
		content = ''
#		publication_date = ''
	return render_to_response('news_edit.html', { 'page_name':page_name, 
'menu_name':menu_name, 'code':code, 'title':title, 'subject':subject, 
'description':description, 'keywords':keywords, 'content':content, 'menu': menu, 
# 'publication_date':publication_date,
})

@login_required
def news_save(request, menu_name, page_name):
	code = request.POST['code']
	title = request.POST['title']
	subject = request.POST['subject']
        description = request.POST['description']
        keywords = request.POST['keywords']
	content = request.POST['content']
#	publication_date = request.POST['publication_date']
	try:
		news = News.objects.get(code=page_name)
		news.code = code
		news.title = title
		news.subject = subject
		news.description = description
		news.keywords = keywords
		news.content = content
#		news.publication_date = publication_date  
	except News.DoesNotExist:
		news = News(code=page_name, title=title, subject=subject,
description=description, keywords=keywords, content=content,
# publication_date=publication_date
)
	news.save()
	page_name = code
	return HttpResponseRedirect('/' + menu_name + '/news/' + page_name + '/')

